/**
 * Created by ZhaoJinBing on 2017/7/24.
 */
;(function ($, layer) {
    var init = function (toolbar) {
        initGrid(toolbar);
    };

    var initGrid = function (toolbar) {
        $('#roleGrid').datagrid({
            url: './role/findAll',
            title: '角色列表',
            method: 'GET',
            idField: 'id',
            pagination: true,
            fitColumns: true,
            striped: true,
            rownumbers: true,
            singleSelect: true,
            toolbar: toolbar,
            columns: [[
                {field: 'roleName', title: '角色名称', width: '30%', halign: 'center'},
                {field: 'description', title: '角色描述', width: '50%', halign: 'center'},
                {field: 'available', title: '是否可用', width: '20%', align: 'center', formatter: function (value) {
                    if (value == '1') {
                        return '<span style="color: #00B83F">可用</span>';
                    } else {
                        return '<span style="color: red">禁用</span>';
                    }
                }}
            ]],
            loadFilter: function (data) {
                var newData = {};
                if (data) {
                    newData.total = data.total;
                    newData.rows = data.list;
                    return newData;
                }
            }
        });
    };

    var initDialog = function (title, callback) {
        $('#roleDialog').dialog({
            title: title,
            modal: true,
            border: 'thin',
            onClose: function () {
                callback();
            }
        })
    };

    var initForm = function (url) {
        var setting = {
            id: 'roleForm',
            url: url,
            method: 'post',
            appendTo: '#roleDialog',
            items: [
                {
                    type: 'textbox',
                    id: 'roleName',
                    name: 'roleName',
                    option: {
                        label: '角色名称',
                        required: true
                    }
                },{
                    type: 'textbox',
                    id: 'description',
                    name: 'description',
                    option: {
                        label: '角色描述',
                        required: true,
                        multiline: true
                    }
                },{
                    type: 'combobox',
                    id: 'available',
                    name: 'available',
                    option: {
                        label: '是否可用:',
                        required: true,
                        data: [{value: 1, text: '可用'}, {value: 0, text: '禁用'}],
                        value: '1'
                    }
                }, {
                    type: 'combotree',
                    id: 'resources',
                    name: 'resources',
                    option: {
                        label: '资源列表:',
                        url: './resource/tree/true',
                        method: 'GET',
                        lines: true,
                        multiple: true,
                        checkbox: true
                    }
                }
            ],
            btns: ['submit["$.role.reloadData"]', 'reset']
        };
        return $.zUtil.form(setting);
    };

    //添加角色
    var add = function () {
        var form = initForm('./role/add');
        initDialog('新增角色', function () {
            form.destroy();
        });
    };

    //分配权限
    var assignPermission = function () {
        var selRow = $('#roleGrid').datagrid('getSelected');
        if (!selRow) {
            layer.msg('请选择一条记录', {icon: 5, time: 2000});
        }
        var dialog = $('<div></div>').appendTo('#rolePage');
        var tree = $('<div></div>').appendTo(dialog);
        tree.tree({
            url: './role/createResourceTree/' + selRow.id,
            method: 'GET',
            lines: true,
            checkbox: true
        });
        dialog.dialog({
            title: '资源列表',
            width: 500,
            height: 400,
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: function () {
                    var nodes = tree.tree('getChecked', ['checked', 'indeterminate']);
                    var resourceIds = new Array();
                    $.each(nodes, function (index, node) {
                        resourceIds.push(node.id);
                    });
                    $.ajax({
                        url: './role/assignPermission',
                        type: 'POST',
                        dataType: 'JSON',
                        data: {
                            roleId: selRow.id,
                            resourceIds: resourceIds.join(',')
                        },
                        success: function (data) {
                            if (data.success) {
                                layer.msg('保存成功', {time: 1000});
                                dialog.dialog('close');
                            } else {
                                layer.msg(data.message, {time: 1000});
                            }
                        }
                    });
                }
            }, {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    dialog.dialog('close');
                }
            }],
            onClose: function () {
                tree.remove();
                dialog.remove();
                tree = null;
                dialog = null;
            }
        });
    };

    //删除角色
    var destroy = function () {
        var selRow = $('#roleGrid').datagrid('getSelected');
        if (!selRow) {
            layer.msg('请选择一条记录', {icon: 5, time: 2000});
        }
        layer.confirm('您确定删除此角色!', {icon: 3, title: '提示'}, function (e) {
            $.ajax({
                url: './role/destroy/' + selRow.id,
                type: 'DELETE',
                dataType: 'JSON',
                success: function (data) {
                    if (data.success) {
                        layer.msg('保存成功', {time: 1000});
                        $('#roleGrid').datagrid('reload');
                        layer.close(e);
                    } else {
                        layer.msg(data.message, {time: 1000});
                    }
                }
            })
        })
    };

    $.extend({
        role: {
            init: init,
            add: add,
            assignPermission: assignPermission,
            destroy: destroy,
            reloadData: function () {
                layer.msg('保存成功！', {time: 1000}, function () {
                    $('#roleDialog').dialog('close');
                    $('#roleGrid').datagrid('reload');
                });
            }
        }
    })
})(jQuery, layer);